package com.huisou.basedataservice;

import com.huisou.domain.Area;
import com.huisou.orm.HibernateDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class AreaDao extends HibernateDao<Area, Integer> {
	protected Logger					log	= LoggerFactory.getLogger(getClass());
	private JdbcTemplate				readJdbcTemplate;
	
	@Resource
	RedisTemplate<String, List<Area>>	redisListTemplate;
	@Resource
	private StringRedisTemplate			stringRedisTemplate;
	
	public List<Area> queryAreaListByPid(int parentId) {
		String key = RedisConstants.KEY_AREA_LIST_PID + "_" + parentId;
		List<Area> listAreas = null;
		try {
			listAreas = redisListTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.info("取Area出错:" + e.getMessage());
		}
		if (listAreas == null) {
			try {
				String sql = " select a.ID id,a.area_code code,a.name name from tbl_system_region a where parent_id = ? ";
				sql += " order by a.sort ";
				listAreas = readJdbcTemplate.query(sql, new RowMapper<Area>() {
					public Area mapRow(ResultSet rs, int rowNum) throws SQLException {
						Area pv = new Area();
						pv.setId(rs.getInt("id"));
						pv.setAreaCode(rs.getString("code"));
						pv.setName(rs.getString("name"));
						return pv;
					}
				}, parentId);
				redisListTemplate.opsForValue().set(key, listAreas);
			}
			catch (Exception e) {
				log.info("取Area出错:" + e.getMessage());
			}
		}
		return listAreas;
	}
	
	public List<Area> queryAreaListByPidPinyin(int parentId) {
		List<Area> listAreas = null;
		try {
			String sql = " select a.ID id,a.area_code code,a.name name from tbl_system_region_pinyin a where parent_id = ? ";
			sql += " order by a.sort_pinyin ";
			listAreas = readJdbcTemplate.query(sql, new RowMapper<Area>() {
				public Area mapRow(ResultSet rs, int rowNum) throws SQLException {
					Area pv = new Area();
					pv.setId(rs.getInt("id"));
					pv.setAreaCode(rs.getString("code"));
					pv.setName(rs.getString("name"));
					return pv;
				}
			}, parentId);
		}
		catch (Exception e) {
			log.info("取Area出错:" + e.getMessage());
		}
		return listAreas;
	}
	
	public String getAreaNameById(Integer id) {
		String key = RedisConstants.KEY_AREA_SIGNLE_NAME + "_" + id;
		String areaName = null;
		try {
			areaName = stringRedisTemplate.opsForValue().get(key);
		}
		catch (Exception e) {
			log.error("取单个areaName出错:" + e.getMessage());
		}
		if (StringUtils.isBlank(areaName)) {
			try {
				areaName = this.get(id).getName();
				stringRedisTemplate.opsForValue().set(key, areaName);
			}
			catch (Exception e) {
				log.error("取单个area出错:" + e.getMessage());
			}
		}
		return areaName;
	}
	
	@Resource
	public void setReadDataSource(DataSource dataSource) {
		readJdbcTemplate = new JdbcTemplate(dataSource);
	}
}
